<?php
/**
 * SellerGroupRepo.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-04-23 15:22:05
 * @modified   2023-04-23 15:22:05
 */

namespace Beike\Seller\Repositories;

use Beike\Seller\Models\Seller;
use Beike\Seller\Models\SellerGroup;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

class SellerGroupRepo
{
    /**
     * 创建一个SellerGroup记录
     * @param $data
     * @return Builder|Model
     * @throws Exception
     */
    public static function create($data): Model|Builder
    {
        try {
            DB::beginTransaction();

            $groupData = [
                'fee_show_flat'    => (float) ($data['fee_show_flat']    ?? 0),
                'fee_show_percent' => (float) ($data['fee_show_percent'] ?? 0),
                'fee_sale_flat'    => (float) ($data['fee_sale_flat']    ?? 0),
                'fee_sale_percent' => (float) ($data['fee_sale_percent'] ?? 0),
            ];
            $group =  SellerGroup::query()->create($groupData);

            $descriptions = [];
            foreach ($data['name'] as $locale => $name) {
                $descriptions[$locale] = [
                    'locale' => $locale,
                    'name'   => $name,
                ];
            }
            foreach ($data['description'] as $locale => $description) {
                $descriptions[$locale]['description'] = $description;
            }
            $group->descriptions()->createMany($descriptions);

            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();

            throw $e;
        }

        return $group;
    }

    /**
     * @param $id
     * @param $data
     * @return Builder|Builder[]|Collection|Model
     * @throws Exception
     */
    public static function update($id, $data): Model|Collection|Builder|array
    {
        $group = SellerGroup::query()->find($id);
        if (! $group) {
            throw new Exception("Seller Group id $id 不存在");
        }
        $group->update($data);

        if (isset($data['name']) || isset($data['description'])) {
            $group->descriptions()->delete();
            $descriptions = [];
            foreach ($data['name'] as $locale => $name) {
                $descriptions[$locale] = [
                    'locale' => $locale,
                    'name' => $name,
                ];
            }
            foreach ($data['description'] as $locale => $description) {
                $descriptions[$locale]['description'] = $description;
            }

            $group->descriptions()->createMany($descriptions);
        }

        return $group;
    }

    /**
     * @param $id
     * @return Builder|Builder[]|Collection|Model|null
     */
    public static function find($id): Model|Collection|Builder|array|null
    {
        return SellerGroup::query()->with('descriptions')->findOrFail($id);
    }

    /**
     * @param $id
     * @return void
     */
    public static function delete($id): void
    {
        $defaultSellerGroupId = plugin_setting('multi_seller.default_seller_group_id');
        if ($id == $defaultSellerGroupId) {
            throw new NotAcceptableHttpException(trans('MultiSeller::seller_group.default_cannot_delete'));
        }

        if (Seller::query()->where('seller_group_id', $id)->count()) {
            throw new NotAcceptableHttpException(trans('MultiSeller::seller_group.be_used_cannot_delete'));
        }

        $group = SellerGroup::query()->find($id);
        $group->descriptions()->delete();
        if ($group) {
            $group->delete();
        }
    }

    /**
     * 获取商户组列表
     *
     * @return Builder[]|Collection
     */
    public static function list(): Collection|array
    {
        $builder = SellerGroup::query()->with('description', 'descriptions');

        return $builder->get();
    }
}
